<?php
    // +----------------------------------------------------------------------
    // |
    // +----------------------------------------------------------------------
    // | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.jmzh1688.com/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: lxl
    // | jmzhfsauto
    // +----------------------------------------------------------------------
    namespace app\admin\controller;
    use think\Controller;
    use think\Db;
    use app\admin\model\GjjhModel;
    class Gjjh extends Base {
        public function getWhereFromPara() {
            $param = input('param.');
            $where = [];
            if (array_key_exists('', $param)) {
                $where[''] = $param[''];
            }
            return $where;
        }
        public function _initialize() {
            parent::_initialize();
            $wherep = $this->getWhereFromPara();
            $furl = url('gjjh/index', $wherep);
            $faddurl = url('gjjh/add', $wherep);
            $fediturl = url('gjjh/edit', $wherep);
            $gjjhmodel = new GjjhModel();
            $tabblename = $gjjhmodel->getTable(); //获取表
            $this->assign(['nowurl'=>$furl, 'addurl'=>$faddurl, 'editurl'=>$fediturl, 'tabblename'=>$tabblename]);
        }
        public function getData() {
            $wherep = $this->getWhereFromPara();
            $param = input('param.');
            if (array_key_exists('pageSize', $param)) {
                $limit = $param['pageSize'];
                $offset = ($param['pageNumber']-1) *$limit;
            } else {
                $limit = 100000;
                $offset = 0;
            }
            $gjjhmodel = new GjjhModel();
            $where = getWhereForm($param);
            $where['isdelete'] = 0;
            $whereor = [];
            $where = array_merge($where, $wherep);
            $list = $gjjhmodel->getByWhere($where, $offset, $limit, $whereor);
            foreach($list as $key=>$vo) {
                $list[$key]['operate'] = $this->getRowButton($vo, $wherep);
            }
            $result['total'] = $gjjhmodel->getAllCount($where, $whereor);  // 总数据
            $result['rows'] = $list;
            return $result;
        }
        public function index() {
            $wherep = $this->getWhereFromPara();
            if (request() ->isAjax()) {
                $result = $this->getData();
                return json($result);
            }
            $toolbarshow = $this->getToolBar($wherep);
            $this->assign(['toolbar'=>$toolbarshow]);
            return $this->fetch();
        }
        public function del() {
            $gjjhmodel = new GjjhModel();
            if (request() ->isPost()) {
                $id = input("post.id");
                $msg = $gjjhmodel->delById($id);
                return json(msg($msg['code'], $msg['data'], $msg['msg']));
            }
            return $this->fetch();
        }
        
          public function test() {

            return $this->fetch();
        }
        public function edit() {
            $gjjhmodel = new GjjhModel();
            if (request() ->isPost()) {
                $param = input("post.");
                foreach($param as $key=>$value) {
                    if (is_array($value)) {
                        $param[$key] = implode(",", $param[$key]);
                    }
                }
                $wherep = $this->getWhereFromPara();
                $parastr = "";
                foreach($wherep as $key=>$value) {
                    $parastr = $parastr.$key."=".$value."&";
                }
                if (session('roleid') >5) {
                    $param["ispublic"] = 0;
                }
                $param["borrower"] = session('username');
                $msg = $gjjhmodel->edit($param);
                return json(msg($msg['code'], $msg['data']."?".$parastr, $msg['msg']));
            }
            $id = input('param.id');
            $info = $gjjhmodel->getById($id);
            $this->assign(['info'=>$info]);
            return $this->fetch();
        }
        public function add() {
            if (request() ->isPost()) {
                $gjjhmodel = new GjjhModel();
                $param = input("post.");
                $wherep = $this->getWhereFromPara();
                $param = array_merge($param, $wherep);
                $parastr = "";
                foreach($wherep as $key=>$value) {
                    $parastr = $parastr.$key."=".$value."&";
                }
                foreach($param as $key=>$value) {
                    if (is_array($value)) {
                        $param[$key] = implode(",", $param[$key]);
                    }
                }
                if (session('roleid') >5) {
                    $param["ispublic"] = 0;
                }
                $param["borrower"] = session('username');
                $id = input('param.id');
                $info = $gjjhmodel->getById($id);
                $where = "rfid='".$param["code"]."'";
                $table = "admin_gjrk";
                $targetinfo = Db::name($table) ->where($where) ->find();
                if (!empty($targetinfo)) {
                    $paramtarget["id"] = $targetinfo["id"];
                    $paramtarget["number"] = $targetinfo["number"]-$param["number"];
                    Db::name($table) ->update($paramtarget);
                }
                $msg = $gjjhmodel->add($param);
                return json(msg($msg['code'], $msg['data']."?".$parastr, $msg['msg']));
            }
            return $this->fetch();
        }
        private function getRowButton($vo, $wherep) {
            $id = $vo['id'];
            $idwhere = $wherep;
            $idwhere['id'] = $id;
            $operatestr = '[{"uuid":"guidbc8512","title":"归还","href":"\/admin\/gjjhgh\/edit?id=$id","icon":"","style":"","para":"","isblank":"2","isnouse":0,"width":"300","height":"400"},{"uuid":"edit","title":"编辑","href":"'.url('edit', ['id'=>$id]) .'","icon":"","style":"","para":"","isblank":"0","isnouse":0},{"uuid":"del","title":"删除","href":"javascript:del(this,$id)","icon":"","style":"","para":"","isblank":"0","isnouse":0}]';
            if (is_array($vo)) {
                $arr = $vo;
            } else {
                $arr = $vo->toArray();
            }
            foreach($arr as $key=>$value) {
                $operatestr = str_replace("$".$key, $value, $operatestr);
            }
            $operate = json_decode($operatestr, true);
            $option = getButtonHtml($operate);
            return $option;
        }
        private function getToolBar($where) {
            $operatestr = '[{"uuid":"add","title":"借用","href":"'.url('add', $where) .'","suffix":"","icon":"","style":"btn btn-outline-primary btn-sm ","para2":"","para1":"0","para":"","isblank":"0","isnouse":0}]';
            foreach($where as $key=>$value) {
                $operatestr = str_replace("$".$key, $value, $operatestr);
            }
            $operate = json_decode($operatestr, true);
            $option = getButtonHtml($operate);
            return $option;
        }
        public function getinfofromother_code($rfid) {
            $where = "rfid='".$rfid."'";
            $table = "admin_gjrk";
            $info = Db::name($table) ->where($where) ->find();
            return json($info);
        }
        /*contentadd*/
    }
    